<?php if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}
/**
 * Class account extends MY_Controller
 * @author xiejianwei
 * 移动端用户接口类
 */
class account extends API_Controller {

	public function __construct() {
		parent::__construct();
        $this->load->model('account_info_model','maccountInfo');
        $this->load->model('identity_model','midentity');
        $this->load->model('upload_model','mupload');
        $this->load->model('concern_fans_model','mconcern_fans');
        $this->load->model('User_weixin_model','muser_weixin');
        $this->load->model('Iosconfig_model','miosconfig');
        $this->accountFields = 'id,ctype,phone,uname,board,position,photo,status,company,concerns,fans,activity,reward,balance,claims,zan_money,com_name,com_short';
        $this->accountInfoFields = 'profile,cprofile,card,thumb,industry,good_industry,interested,single_project,tags,fund_stage,fund_scale,business_type,enterprises';
	}

    protected $rules = array(
        "edit" => array(
            array(
                'field' => 'photo',
                'label' => '头像',
                'rules' => 'trim|required|numeric',
            ),
            array(
                "field" => "token",
                "label" => "Token",
                "rules" => "trim|required",
            ),
            array(
                "field" => "realname",
                "label" => "姓名",
                "rules" => "trim|required",
            ),
            array(
                "field" => "area",
                "label" => "地区",
                "rules" => "trim|required",
            )
        ),
        "edit_pwd" => array(
            array(
                "field" => "token",
                "label" => "Token",
                "rules" => "trim|required",
            ),
            array(
                'field' => 'oldpassword',
                'label' => '原始密码',
                'rules' => 'trim|required|min_length[6]|callback_oldpassword_check',
            )
            , array(
                'field' => 'password',
                'label' => '密码',
                'rules' => 'trim|required|min_length[6]',
            )
            , array(
                'field' => 'password_re',
                'label' => '确认密码',
                'rules' => 'trim|required|min_length[6]|callback_pwd_check',
            )
        ),
        "get_token" => array(
            array(
                "field" => "id",
                "label" => "编号ID",
                "rules" => "trim|required",
            )
        ),
        "identity" => array(
            array(
                "field" => "ctype",
                "label" => "变更原因",
                "rules" => "trim|required|callback_identity_check",
            ),
            array(
                'field' => 'com_name',
                'label' => '变更公司全称',
                'rules' => 'trim|required|callback_comName_check',
            ),
            array(
                'field' => 'com_short',
                'label' => '变更公司简称',
                'rules' => 'trim|required',
            ),
            array(
                'field' => 'photo',
                'label' => '名片',
                'rules' => 'trim|required|callback_photo_check',
            )
        ),
        "ry_get_token" => array(
            array(
                "field" => "id",
                "label" => "账号编号",
                "rules" => "trim|required"
            )
        )
    );

// 会员详细信息
    public function info() {

        if (!isset($this->data['terminalNo'])) {
            $this->data['terminalNo'] = 3;
        }
        // 返回用户详细数据
        if(isset($this->data['uid']) && $this->data['uid']) {
            $info = $this->mvacc->get_info(array('id' => $this->data['uid']), 'nofresh', $this->data['terminalNo'], $this->accountFields);
            $uid = $this->data['uid'];
            $flag = false;
        } else {
            $this->_auto();
            $info = $this->mvacc->get_info(array('id' => $this->userinfo['id']), 'nofresh', $this->data['terminalNo'], $this->accountFields);
            $uid = $this->userinfo['id'];
            $flag = true;
        }

        if ($info) {
            $time = isset($this->data['time']) && $this->data['time'] ? $this->data['time'] : 0;
            $info['activity'] = user_activities($uid,$time);
            $info_data = $this->maccountInfo->get_one(array('uid'=>$uid),$this->accountInfoFields);
            $info_data['profile'] = $info_data['profile'] ? $info_data['profile'] : '';
            $info_data['cprofile'] = $info_data['cprofile'] ? $info_data['cprofile'] : '';
            $info_data['enterprises'] = $info_data['enterprises'] ? unserialize($info_data['enterprises']) : '';
            switch($info['ctype']) {
                case 1:
                    $info_data['industry'] = $info_data['industry'] ? get_mcategory_titles($info_data['industry']) : '';
                    $info_data['tags'] = $info_data['tags'] ?  get_mcategory_titles($info_data['tags']) : '';
                    break;
                case 2:
                    $info_data['interested'] = $info_data['interested'] ? get_mcategory_titles($info_data['interested']) :'';
                    $info_data['fund_stage'] = $info_data['fund_stage'] ? get_mcategory_titles($info_data['fund_stage']) :'';
                    $info_data['single_project'] = $info_data['single_project'] ? get_mcategory_titles($info_data['single_project']) : '';
                    break;
                case 3:
                    $info_data['industry'] = $info_data['industry'] ? get_mcategory_titles($info_data['industry']) : '';
                    $info_data['interested'] =  $info_data['interested'] ? get_mcategory_titles($info_data['interested']) : '';
                    break;
                case 4:
                    $info_data['good_industry'] = $info_data['good_industry'] ?  get_mcategory_titles($info_data['good_industry']) : '';
                    $info_data['interested'] = $info_data['interested'] ? get_mcategory_titles($info_data['interested']) : '';
                    break;
                case 5:
                    $info_data['interested'] = $info_data['interested'] ? get_mcategory_titles($info_data['interested']) : '';
                    $info_data['good_industry'] = $info_data['good_industry'] ? get_mcategory_titles($info_data['good_industry']) : '';
                    break;
                case 6:
                    //$info_data['business_type'] = get_mcategory_titles($info_data['business_type']);
                    $info_data['interested'] = $info_data['interested'] ? get_mcategory_titles($info_data['interested']) : '';

                    break;
            }
            $this->load->model('company_model','mcompany');
            $company = $this->mcompany->get_one($info['company'],'name,desc,short,short_desc');
            if($company) {
                $company['desc'] = content_br($company['desc']);
                $company['short_desc'] = content_br($company['short_desc']);
            }
            if($info['ctype'] == 1) {
                $info_data['cprofile'] = $company['desc'];
            }
            $rep = $this->mcompany->get_one(array('id'=>$info['company'],'uid'=>$uid));
            $info['company'] = $company ? array('com_name'=>$company['name'],'com_id'=>$info['company'],'short_desc'=>content_br($company['short_desc'])) : array('com_name'=>$info['com_name']);
            $info['info'] = $info_data;

            $card =  $info_data['card'] ? one_upload($info_data['card']) : '';
            $info['info']['card'] =$card ?  UPLOAD_URL.$card['url'] :'';
            $fans = 0;
            if($this->userinfo) {
                if(isset($this->data['uid']) && $this->data['uid']) {
                    $fans = $this->mconcern_fans->get_one(array('uid'=>$this->userinfo['id'],'rid'=>$this->data['uid'],'is_deleted'=>1));
                }
            }
            $total = $this->mconcern_fans->get_new_nums($this->userinfo['id']);
            $info['new_fans_nums'] = $total['total'];           //新粉丝数
            //是否关注
            $info['is_fans'] = $fans ? 1 : 0;
            //是否为企业代表

            $info['is_rep'] = $rep ? 1 : 0;
            //是否能变更身份
            $identity = $this->midentity->get_one(array('uid'=>$uid));
            $info['identity'] = $identity && $identity['status']==1 ? 1 : 0;
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作成功';
            $this->vdata['content'] = $info;
        } else {
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作失败';
        }
        // 返回json数据
        $this->_send_json($this->vdata);
    }
    public function editPhoto() {
        $this->_auto();
        if(isset($this->data['photo']) && $this->data['photo']) {
            $this->load->model('upload_model','mupload');
            $photo = $this->mupload->get_one($this->data['photo'],'id,url,thumb');
            $info = $this->macc->update(array('photo'=>$this->data['photo'],'thumb'=>$photo['thumb']),array('id'=>$this->userinfo['id']));
            if ($info) {
                // 返回成功
                $this->vdata['returnCode'] = '200';
                $this->vdata['returnInfo'] = '操作成功';
                $this->vdata['secure'] = JSON_SECURE;
                $this->vdata['content'] = $info;
            } else {
                // 返回失败
                $this->vdata['returnCode'] = '200';
                $this->vdata['returnInfo'] = '操作失败';
                $this->vdata['secure'] = JSON_SECURE;
                $this->vdata['content'] = '';
            }

            $this->_send_json($this->vdata);
        }
    }
	// // 个人中心-设置头像
	public function edit() {
        $this->_auto();
        $flag = false;
        if(isset($this->data['photo']) && $this->data['photo']) {
            $this->load->model('upload_model','mupload');
            $photo = $this->mupload->get_one($this->data['photo'],'id,url,thumb');
            $this->macc->update(array('photo'=>$this->data['photo'],'thumb'=>$photo['thumb']),array('id'=>$this->userinfo['id']));
            $flag = true;
        }
        if(isset($this->data['board']) && $this->data['board']) {
            $this->macc->update(array('board'=>$this->data['board']),array('id'=>$this->userinfo['id']));
            $flag = true;
        }
        $enterprises = false;
        switch($this->userinfo['ctype']) {
            case 1:
                $data = array('tags'=>$this->data['tags']);
                break;
            case 2:
                $enterprises = true;
                $data = array('interested' => $this->data['interested'],'fund_stage'=>$this->data['fund_stage'],'single_project'=>$this->data['single_project']);
                break;
            case 3:
                $data = array('industry'=>$this->data['industry'],'interested' => $this->data['interested']);
                break;
            case 4:
                $enterprises = true;
                $data = array('good_industry' => $this->data['good_industry'],'interested' => $this->data['interested']);
                break;
            case 5:
                $enterprises = true;
                $data = array('interested' => $this->data['interested'],'good_industry' => $this->data['good_industry']);

                break;
            case 6:
                $enterprises = true;
                $data = array('interested' => $this->data['interested']);
                break;
        }
        if(isset($this->data['profile']) && $this->data['profile']) {
            $data['profile'] = $this->data['profile'];
        }
        if(isset($this->data['cprofile']) && $this->data['cprofile']) {
            $data['cprofile'] = $this->data['cprofile'];
        }
        if($enterprises) {
            if(isset($this->data['enterprises']) && $this->data['enterprises']) {
                $data['enterprises'] = serialize($this->data['enterprises']);
            } else{
                $data['enterprises'] = '';
            }
        }

        $this->maccountInfo->update($data,array('uid'=>$this->userinfo['id']));

        if($flag) {
            $this->macc->update(array('updatetime'=>time()),array('id'=>$this->userinfo['id']));
        }
        if ($info = $this->mvacc->get_info(array('id' => $this->userinfo['id']), 'nofresh', $this->data['terminalNo'], $this->accountFields)) {
            $info_data = $this->maccountInfo->get_one(array('uid'=>$this->userinfo['id']),$this->accountInfoFields);
            $info['info'] = $info_data;
            // 返回成功
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作成功';
            $this->vdata['secure'] = JSON_SECURE;
            $this->vdata['content'] = $info;
        } else {
            // 返回失败
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作失败';
            $this->vdata['secure'] = JSON_SECURE;
            $this->vdata['content'] = '';
        }

		$this->_send_json($this->vdata);
	}
    //返回标签
    public function other() {
        $this->_auto();

        switch($this->userinfo['ctype']) {
            case 1:
                $ctype = array(25);
                $param = array('tags');
                break;
            case 2:
                $ctype = array(24,26,27);
                $param = array('interested','fund_stage','single_project');
                break;
            case 3:
                $ctype = array(24,24);
                $param = array('industry','interested');
                break;
            case 4:
                $ctype = array(24,24);
                $param = array('good_industry','interested','enterprises');
                break;
            case 5:
                $ctype = array(24,24);
                $param = array('interested','good_industry','enterprises');
                break;
            case 6:
                $ctype = array(24);
                $param = array('interested','enterprises');
                break;
        }
        $this->load->model('category_model','mcategory');
        $arr_cates = array();
        foreach($param as $val) {
            $info = $this->maccountInfo->get_one(array('uid'=>$this->userinfo['id']),$val);
            $arr_cates[$val] = $info ? explode(',',$info[$val]) : '';
        }
        $where = array('cid' => 3, 'audit' => 1);
        foreach($ctype as $ke =>$va) {
            $file_ids = $arr_cates[$param[$ke]];
            $where_in = array('ctype'=>$va);
            $where = array_merge($where,$where_in);
            $categorys = $this->mcategory->get_all($where,'id,title');
            foreach($categorys as $key=>$row) {
                $arr = ($file_ids && in_array($row['id'],$file_ids)) ? array('is_select'=>1) : array('is_select'=>0);
                $data[$param[$ke]][$key] = array_merge($row,$arr);
            }
        }
        $this->vdata['returnCode'] = '200';
        $this->vdata['returnInfo'] = '操作成功';
        $this->vdata['secure'] = JSON_SECURE;
        $this->vdata['content'] = $data;
        $this->_send_json($this->vdata);
    }
    // 个人中心-更改登录密码
    public function editPwd() {
        $this->_auto();
        // 验证
        $this->form_validation->set_rules($this->rules['edit_pwd']);
        // validate验证结果
        if ($this->form_validation->run('', $this->data) == false) {
            // 返回失败
            $this->vdata['returnCode'] = '0011';
            $this->vdata['returnInfo'] = $this->trim_validation_errors();
        } else {
            if ($res = $this->macc->set($this->userinfo['id'], array('pwd' => passwd($this->data['password'])))) {
                // 返回成功
                $this->vdata['returnCode'] = '200';
                $this->vdata['returnInfo'] = '操作成功,请重新登录';
                $this->vdata['secure'] = JSON_SECURE;
                $this->vdata['content']= $res;
            } else {
                // 返回失败
                $this->vdata['returnCode'] = '0011';
                $this->vdata['returnInfo'] = '更新失败,请刷新后重试';
                $this->vdata['secure'] = JSON_SECURE;
                $this->vdata['content'] = '';
            }
        }
        // 返回json数据
        $this->_send_json($this->vdata);
    }
    public function get_token()
    {
        // 验证
        $this->form_validation->set_rules($this->rules['get_token']);
        // validate验证结果
        if ($this->form_validation->run('', $this->data) == false) {
            // 返回失败
            $this->vdata['returnCode']   = '0011';
            $this->vdata['returnInfo'] = validation_errors();
        } else {
            if (!isset($this->data['terminalNo'])) {
                $this->data['terminalNo'] = 0;
            }
            // 返回用户详细数据
            if ($info = $this->mvacc->get_info(array('id' => $this->data['id']), 'nofresh', $this->data['terminalNo'])) {
                $this->vdata['returnCode'] = '200';
                $this->vdata['returnInfo'] = '操作成功';
                $this->vdata['secure'] = JSON_SECURE;
                $this->vdata['content'] = $info;
            } else {
                $this->vdata['returnCode'] = '200';
                $this->vdata['returnInfo'] = '操作失败';
            }
        }
        // 返回json数据
        $this->_send_json($this->vdata);
    }
    //用户身份变更
    public function identity() {
        $this->_auto();
        $this->form_validation->set_rules($this->rules['identity']);
        if ($this->form_validation->run('', $this->data) == false) {
            $this->vdata['returnCode'] = '0011';
            $this->vdata['returnInfo'] = $this->trim_validation_errors();
        } else {
            $data = array(
                'ctype' => $this->data['ctype'],
                'com_name' => $this->data['com_name'],
                'com_short' => $this->data['com_short'],
                'photo' => $this->data['photo'],
                'uid' => $this->userinfo['id'],
            );
            if($this->data['ctype'] == 34) {
                $data = array_merge($data,array(
                    'uctype' => $this->userinfo['ctype'],
                    'actype' => $this->data['actype'],
                    'uname' => $this->userinfo['uname'],
                    'position' => $this->data['position']
                ));
            }
            $this->load->model('company_model','mcompany');
            $company = $this->mcompany->get_one($this->userinfo['company']);
            $data['old_com_name'] = $company['name'];
            $data['old_com_short'] = $company['short'];
            $data['photo'] = $this->data['photo'];
            $photo = one_upload($this->data['photo']);
            $data['thumb'] = $photo['url'];
            $result = $this->midentity->create($data);
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作成功';
            $this->vdata['secure'] = JSON_SECURE;
            $this->vdata['content'] = $result;
        }
        $this->_send_json($this->vdata);
    }

    public function oldpassword_check($str) {
        if (isset($this->userinfo) && ($this->userinfo['pwd'] == passwd($str))) {
            return true;
        }
        $this->form_validation->set_message('oldpassword_check', '原密码错误！');
        return false;
    }
    public function pwd_check($str) {
        if (isset($str) && ($str == $this->input->post('password'))) {
            return true;
        }
        $this->form_validation->set_message('pwd_check', '两次密码不一致');
        return false;
    }
    public function feedback() {
        $this->_auto();
        $this->load->model('feedback_model', 'mfeedback');
        $create_data = array(
            'content'   => $this->data['content'],
            'tel'       => $this->data['tel'],
            'timeline'  => time()
        );
        // 创建数据
        if ($id = $this->mfeedback->create($create_data)) {
            $msgs = array(
                'timeline'  =>  time(),
                'type'      =>  3,
                'uid'       =>  $this->userinfo['id'],
                'rid'       =>  $id
            );
            $this->load->model('msgs_model', 'mmsgs');
            $this->mmsgs->create($msgs);
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作成功';
            $this->vdata['secure'] = JSON_SECURE;
            $this->vdata['content'] = $id;
        } else {
            $this->vdata['returnCode'] = '0011';
            $this->vdata['returnInfo'] = '操作失败';
        }
        $this->_send_json($this->vdata);
    }
    //检查图片
    public function photo_check($photo) {
        // 查找用户
        $photo_check = $this->mupload->get_one($photo);
        if (!$photo_check) {
            $this->form_validation->set_message('photo_check', '上传名片路径有误');
            return false;
        } else {
            return true;
        }
    }
    /**
     * 检查用户有无完成第三步
     */
    public function rsuccess() {
        $this->_auto();
        $info = $this->mvacc->get_info(array('id' => $this->userinfo['id']), 'nofresh', $this->data['terminalNo'], $this->accountFields);
        $info_data = $this->maccountInfo->get_one(array('uid'=>$this->userinfo['id']),$this->accountInfoFields);
        //var_dump($this->userinfo['id']);die();
        $success = 0;
        if($info_data) {
            switch($info['ctype']) {
                case 1:
                    $success = $info_data['industry'] && $info_data['tags'] ? 1 : 0;
                    break;
                case 2:
                    $success = $info_data['interested'] && $info_data['fund_stage'] && $info_data['single_project'] ? 1 : 0;
                    break;
                case 3:
                    $success = $info['board'] && $info_data['industry'] && $info_data['interested'] ? 1 : 0;
                    break;
                case 4:
                    $success = $info_data['good_industry'] && $info_data['interested'] ? 1 : 0;
                    break;
                case 5:
                    $success = $info_data['interested'] ? 1 : 0;
                    break;
                case 6:
                    //$info_data['business_type'] = get_mcategory_titles($info_data['business_type']);
                    $success = $info_data['interested'] ? 1 : 0;
                    break;
            }
        }
        //检测身份变更是否通过
        $this->load->model('identity_model','midentity');
        $identity_one = $this->midentity->get_one(array('uid'=>$this->userinfo['id']),'status');
        $identity = $identity_one && $identity_one['status'] == 1 ? 1 : 0;

        $this->vdata['returnCode'] = '200';
        $this->vdata['returnInfo'] = '操作成功';
        $this->vdata['secure'] = JSON_SECURE;
        $this->vdata['content'] = array('is_success'=>$success,'ctype'=>$info['ctype'],'identity'=>$identity);

        $this->_send_json($this->vdata);
        //return $success;
    }
    /**
     * 举报
     */
    public function report() {
        $this->_auto();
        $data = array(
            'uid'=>$this->userinfo['id'],
            'rid'=>$this->data['rid'],
            'ctype'=>$this->data['ctype'],
            'content'=>$this->data['content'],
            'timeline'=>time()
        );
        $this->load->model('report_model','mreport');
        $inster = $this->mreport->create($data);
        // 返回用户详细数据
        if ($inster) {
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '举报成功';
            $this->vdata['secure'] = JSON_SECURE;
            $this->vdata['content'] = $inster;
        } else {
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作失败';
        }

        // 返回json数据
        $this->_send_json($this->vdata);
    }
    public function reportCtype() {
        $this->_auto();
        $this->load->model('coltypes_model','mcoltypes');
        $inster = $this->mcoltypes->get_all(array('cid'=>21),'id,title');
        // 返回用户详细数据
        if ($inster) {
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作成功';
            $this->vdata['secure'] = JSON_SECURE;
            $this->vdata['content'] = $inster;
        } else {
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作失败';
        }

        // 返回json数据
        $this->_send_json($this->vdata);
    }
    //融云---token
    public function RygetToken()
    {
        // 验证
        $this->form_validation->set_rules($this->rules['ry_get_token']);
        // validate验证结果
        if ($this->form_validation->run('', $this->data) == false) {
            // 返回失败
            $this->vdata['returnCode']   = '0011';
            $this->vdata['returnInfo'] = validation_errors();
        } else {
            include LIBS_PATH.'ry/rongcloud.php';
            $RongCloud = new RongCloud(RY_APPKEY,RY_APPSECRET);

            // 返回用户详细数据
            if ($finfo=$this->macc->get_one(array('id' => $this->data['id']),'id,uname,photo')) {
                if($finfo['photo']) {
                    $purl = one_upload($finfo['photo']);
                    $photourl = UPLOAD_URL.$purl['url'];
                } else {
                    $photourl = static_file('avatar.png');
                }
                $result = $RongCloud->user()->getToken($finfo['id'], $finfo['uname'], $photourl);
                $res=json_decode($result);
                logfile('rongyun Data: '.print_r($result, 1), 'api/post_data_rongyun_');
                if(!empty($res->token)){
                    $this->vdata['returnCode'] = '200';
                    $this->vdata['returnInfo'] = '操作成功';
                    $this->vdata['secure'] = JSON_SECURE;
                    $this->vdata['content']['result'] = $res;
                    $this->vdata['content']['account'] = array('nickname'=>$finfo['uname'],'photo'=>$photourl);
                }else{
                    $this->vdata['returnCode'] = '200';
                    $this->vdata['returnInfo'] = '操作失败';
                }

            } else {
                $this->vdata['returnCode'] = '200';
                $this->vdata['returnInfo'] = '操作失败';
            }
        }
        // 返回json数据
        $this->_send_json($this->vdata);
    }
    //根据时长计算活跃度
    public function activity() {
        $this->_auto();
        $this->macc->set($this->userinfo['id'],array('live'=>$this->userinfo['live']+$this->data['time']));
        $activity = user_activities($this->userinfo['id'],$this->userinfo['live']);
        //$info = $this->macc->update(array('activity'=>$activity),array('id'=>$this->userinfo['id']));
        $info = $this->macc->set($this->userinfo['id'],array('activity'=>$activity));
        if ($info) {
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作成功';
            $this->vdata['secure'] = JSON_SECURE;
            $this->vdata['content'] = $activity;
        } else {
            $this->vdata['returnCode'] = '10003';
            $this->vdata['returnInfo'] = '您的活跃度没变化';
        }

        // 返回json数据
        $this->_send_json($this->vdata);
    }
    //验证身份审核是否可提交
    public function identity_check($ctype) {
        $uid = $this->userinfo['id'];
        //审核状态0 提交审核 1 审核不通过 2 审核通过
        if($ctype == 34) {//工作单位变更
            $this->load->model('company_model','mcompany');
            $transfer = $this->mcompany->get_one(array('uid'=>$uid),'id');
            if($transfer) {
                $this->form_validation->set_message('identity_check', '请先将您的企业代表身份转让');
                return false;
            }
        }
        $identity = $this->midentity->get_one(array('uid'=>$uid,'status'=>0));
        if($identity) {
            $this->form_validation->set_message('identity_check', '您的身份变更正在审核，请不要重复提交');
            return false;
        } else {
            $account = $this->macc->get_one($uid);
            if($account['status'] == 1) {
                $this->form_validation->set_message('identity_check', '您的帐号正在审核，不能进行身份变更');
                return false;
            }
            return true;
        }
    }
    //身份变更验证公司名称
    public function comName_check($com_name) {
        $this->load->model('company_model','mcompany');
        if($_POST['ctype'] == 33) {
            $company_one = $this->mcompany->get_one(array('name'=>$com_name,'or'=>array('short'=>$_POST['com_short'])));
            if($company_one) {
                $this->form_validation->set_message('comName_check', '您填写的公司名称或简称重复');
                return false;
            }
        }
        return true;
    }


    /**
     * 有没有新粉丝、新朋友
     */
    public function new_friend_fans(){
        $this->_auto();
        $uid = $this->userinfo['id'];
        $total = $this->mconcern_fans->get_new_nums($uid);
        $fans = $total['total'];            //新粉丝数量
        //$friend = 0;                        //新朋友数量
        //统计下这里
        $friend = $this->count_friend();
        if($fans || $friend){
            $all = $fans + $friend;
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作成功';
            $this->vdata['secure'] = JSON_SECURE;
            $this->vdata['content'] = array('all_nums'=>$all,'fans'=>$fans,'friend'=>$friend);
        }elseif(!$fans && !$friend){
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作成功';
            $this->vdata['secure'] = JSON_SECURE;
            $this->vdata['content'] = array('all_nums'=>0,'fans'=>0,'friend'=>0);
        }
        // 返回json数据
        $this->_send_json($this->vdata);
    }
    /**
     * 统计新朋友
     */
    public function count_friend() {
        /*$uids = array();
        $rids = array();
        $users = $this->macc->get_all(array('concerns >'=>10,'audit'=>0,'id <> '=>$this->userinfo['id']),'id');
        if($users) {
            foreach($users as $row) {
                $uids[] = $row['id'];
            }
        }
        $concerns = $this->mconcern_fans->get_all(array('uid'=>$this->userinfo['id'],'is_deleted'=>1),'rid');
        $count_concerns = count($concerns);
        if($concerns && $count_concerns > 10) {
            foreach($concerns as $val) {
                $rids[] = $val['rid'];
            }
        }
        $ids = array();
        if($uids && $rids) {
            $ids = $this->mconcern_fans->get_similarity_uids($uids, $rids);
        }*/
        $phones = isset($this->data['phones']) ? $this->data['phones'] : '';
        //$phone_ids = $this->mconcern_fans->get_phone_ids($phones, $this->userinfo, $ids);
        if($phones) {
            $phone_ids = $this->mconcern_fans->get_phone_id($phones, $this->userinfo['phone']);
            $count = count($phone_ids);
            $f_ids = array();
            $phone_ids = assoc_unique( $phone_ids,'id');
            foreach ($phone_ids as $row) {
                $fans = $this->mconcern_fans->get_one(array('uid' => $this->userinfo['id'], 'rid' => $row['id'],'is_deleted'=>1), 'id');
                if($fans) continue;
                $f_ids[] = $row['id'];
            }
            return count($f_ids);
        } else {
            return 0;
        }

    }


    /**
     * 是否绑定微信
     */
    public function account_bind(){
        $this->_auto();
        $uid = $this->userinfo['id'];
        $query = $this->muser_weixin->get_one(array('uid'=>$uid));
        if(!empty($query)){
            if($query['openid']!=''){
                $this->vdata['returnCode'] = '200';
                $this->vdata['returnInfo'] = '已绑定';
                $this->vdata['secure'] = JSON_SECURE;
                $this->vdata['content'] = array('openid'=>$query['openid'],'uid'=>$uid,'is_bind'=>1);
            }else{
                $this->vdata['returnCode'] = '200';
                $this->vdata['returnInfo'] = '未绑定';
                $this->vdata['secure'] = JSON_SECURE;
                $this->vdata['content'] = array('is_bind'=>0);
            }
        }else{
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '未绑定';
            $this->vdata['secure'] = JSON_SECURE;
            $this->vdata['content'] = array('is_bind'=>0);
        }
        // 返回json数据
        $this->_send_json($this->vdata);
    }


    public function bind_weixin(){
        $this->_auto();
        $uid = $this->userinfo['id'];
        $params = $this->input->post();
        if(empty($params)){
            $this->vdata['returnCode'] = '250';
            $this->vdata['returnInfo'] = '缺少参数';
            $this->vdata['secure'] = JSON_SECURE;
            $this->vdata['content'] = array();
        }else{
            $openid = $params['openid'];
            $query = $this->muser_weixin->get_one(array('uid'=>$uid,'openid'=>$openid));
            if(empty($query)){
                $create_data = array(
                    'uid'=>$uid,
                    'openid'=>$params['openid'],
                    'unionid'=>$params['unionid'],
                    'nickname'=>$params['nickname'],
                    'sex'=>$params['sex'],
                    'headimgurl'=>$params['headimgurl'],
                    'timeline'=>time()
                );
                if($this->muser_weixin->create($create_data)){
                    $this->vdata['returnCode'] = '200';
                    $this->vdata['returnInfo'] = '绑定成功';
                    $this->vdata['secure'] = JSON_SECURE;
                    $this->vdata['content'] = array('openid'=>$openid,'uid'=>$uid);
                }else{
                    $this->vdata['returnCode'] = '250';
                    $this->vdata['returnInfo'] = '绑定失败';
                    $this->vdata['secure'] = JSON_SECURE;
                    $this->vdata['content'] = array();
                }
            }else{
                $this->vdata['returnCode'] = '250';
                $this->vdata['returnInfo'] = '已绑定，不可重复操作';
                $this->vdata['secure'] = JSON_SECURE;
                $this->vdata['content'] = array();
            }
        }
        // 返回json数据
        $this->_send_json($this->vdata);
    }

    //检查ios版本控制开关
    public function iosconfig(){
        $title= $this->input->post('title');
        if(!$title){
            $this->vdata['returnCode'] = '250';
            $this->vdata['returnInfo'] = '缺少参数';
            $this->vdata['secure'] = JSON_SECURE;
            $this->vdata['content'] = array();
        }else{
            $config_info = $this->miosconfig->get_one(array('title'=>$title));
            if(empty($config_info)){
                $this->vdata['returnCode'] = '200';
                $this->vdata['returnInfo'] = '请求成功';
                $this->vdata['secure'] = JSON_SECURE;
                $this->vdata['content'] = array('is_can'=>1);
            }else{
                if($config_info['status']==1){
                    $this->vdata['returnCode'] = '200';
                    $this->vdata['returnInfo'] = '请求成功';
                    $this->vdata['secure'] = JSON_SECURE;
                    $this->vdata['content'] = array('is_can'=>1);
                }else{
                    $this->vdata['returnCode'] = '200';
                    $this->vdata['returnInfo'] = '请求成功';
                    $this->vdata['secure'] = JSON_SECURE;
                    $this->vdata['content'] = array('is_can'=>0);
                }
            }
        }
        // 返回json数据
        $this->_send_json($this->vdata);
    }
    //个推 --- 绑定设备id
    public function devicetoken() {
        $this->_auto();
        $data = array(
            'uid' => $this->userinfo['id'],
            'devicetoken' => $this->data['devicetoken'],
            'devicecid' => $this->data['devicecid'],
        );
        $token = array(
            'devicetoken' => $this->data['devicetoken'],
            'devicecid' => $this->data['devicecid'],
        );
        $this->load->model('getui_model','mgetui');
        $device = $this->mgetui->get_all($token,'id');
        $device_ids = array();
        if(!$device) {
            $device_ids1 = array();
            $account_device = $this->mgetui->get_all(array('uid'=>$this->userinfo['id']),'id');
            if($account_device) {
                foreach($account_device as $row1) {
                    $device_ids1[] = $row1['id'];
                }
                $this->mgetui->del($device_ids1);
            }

            $getui = $this->mgetui->create($data);
        } else {
            foreach($device as $row) {
                $device_ids[] = $row['id'];
            }
            $this->mgetui->del($device_ids);
            $getui = $this->mgetui->create($data);
        }

        $this->vdata['returnCode'] = '200';
        $this->vdata['returnInfo'] = '绑定成功';
        $this->vdata['secure'] = JSON_SECURE;
        $this->vdata['content'] = $getui;
        $this->_send_json($this->vdata);
    }
    public function ryAppKey() {
        $this->_auto();
        $ry = array(
            'Appsecret'=>'NTrot9ZAKnw',
            'Appkey'=>'vnroth0kvqrxo',
        );
        $this->vdata['returnCode'] = '200';
        $this->vdata['returnInfo'] = '请求成功';
        $this->vdata['secure'] = JSON_SECURE;
        $this->vdata['content'] = $ry;
        $this->_send_json($this->vdata);
    }
}
